package handle;

import enums.FruitType;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

/**
 * @Description:顾客在超市购买水果处理
 * 设计模式 使用建造者模式
 * @author: panHui.zhang
 * @date: 2024/5/6
 */
public class SupermarketHandle {
    private final Shopping shopping;

    private final static BigDecimal FILL_HUNDRED = BigDecimal.valueOf(100); //优惠标准
    private final static BigDecimal Fill_HUNRED_REDUCE = BigDecimal.valueOf(10); //优惠额度

    private SupermarketHandle(Shopping shopping) {
        this.shopping = shopping;
    }

    //计算商品总价
    public BigDecimal totalPrice() {
        HashMap<String, Double> shoppingTrolley = shopping.getShoppingTrolley();//获得当前的购物车
        BigDecimal totalAmount = new BigDecimal("0.00");//初始总价格
        for (String fruitName : shoppingTrolley.keySet()) {
            Double weight = shoppingTrolley.get(fruitName);
            validWeight(weight);//判断商品重量是否合格
            BigDecimal singleAmount = shopping.getFruitPrice(fruitName).multiply(new BigDecimal(weight)).setScale(2, RoundingMode.HALF_UP); //得出每种商品所需要的价格
            totalAmount = totalAmount.add(singleAmount);
        }
        return totalAmount;
    }


    //计算促销后的商品总价
    public BigDecimal discountTotalPrice() {
        BigDecimal totalAmount = totalPrice();
        if(totalAmount.compareTo(FILL_HUNDRED) >=0){
            return totalAmount.subtract(Fill_HUNRED_REDUCE);
        };

        return totalAmount;
    }

    /**
     * @param weight 商品重量
     * @desc 验证商品重量是否合法
     */
    private void validWeight(Double weight) {
        if (weight < 0) {
            throw new RuntimeException("购买水果斤数不能小于0");
        }
    }

    /**
     * @desc 内部商品信息设置 不对外提供修改
     * 用户生成当前超市可以售卖那些水果以及价格
     */
    public static class Shopping {
        //保存商品对应的价格
        private final HashMap<String, BigDecimal> goods = new HashMap<>();
        //用户的购物车
        private final HashMap<String, Double> shoppingTrolley = new HashMap<>();

        public Shopping() {
            initGoods();
        }

        //初始化商品信息
        private void initGoods() {
            for (FruitType fruit : FruitType.values()) {
                goods.put(fruit.getFruitName(), new BigDecimal(fruit.getPrice()));
            }
        }

        public SupermarketHandle build() {
            return new SupermarketHandle(this);
        }

        /***
         * 顾客购买水果方法
         * @param fruit 水果类型
         * @param weight 水果重量
         * @return
         */
        public Shopping buy(FruitType fruit, double weight) {
            shoppingTrolley.put(fruit.getFruitName(), weight);
            return this;
        }

        /**
         * @param fruit 水果类型
         * @param weight 水果重量
         * @param discount 是否是商品折扣
         * @return
         */
        public Shopping buy(FruitType fruit, double weight, Boolean discount) {
            shoppingTrolley.put(fruit.getFruitName(), weight);
            if (discount) {//该商品进行折扣，计算出折扣后的价格
                goods.put(fruit.getFruitName(), getGoodsPromotionPrice(fruit));
            }
            return this;
        }


        /**
         * 顾客购买商品的价格
         *
         * @param fruit
         * @return
         */
        private BigDecimal getGoodsPromotionPrice(FruitType fruit) {
            return new BigDecimal(fruit.getPrice()).multiply(new BigDecimal(fruit.getPromotionPrice()));
        }

        private HashMap<String, Double> getShoppingTrolley() {
            return shoppingTrolley;
        }

        private BigDecimal getFruitPrice(String fruitName) {
            return goods.get(fruitName);
        }
    }
}
